package com.loan.common.pay.omi;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSONObject;
import com.loan.common.utils.HttpUtil;
import org.apache.commons.codec.digest.Md5Crypt;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.*;
import java.security.spec.KeySpec;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;

public class OMIUtils {

    /**
     * 沙盒环境  代付
     */
    private static final String SANDBOX_PAYMENT_URL = "https://account-sandbox.omipay.vn/api/ibft/create";

    /**
     * 代付
     */
    private static final String PAYMENT_URL = "https://account.omipay.vn/api/ibft/create";

    private static final String SANDBOX_CHECK_USER_BANK = "https://account-sandbox.omipay.vn/api/ibft/CheckUserBank";

    private static final String check_user_bank = "https://account.omipay.vn/api/ibft/CheckUserBank";

    private static final String SANDBOX_CREATE_VA = "https://account-sandbox.omipay.vn/api/va/create";

    private static final String CREATE_VA = "https://account.omipay.vn/api/va/create";

    private static final String REQUEST_ID = "";

    private final static String CRYPTO_METHOD = "RSA";

    private final static String PRIVATE_KEY = "private_key";

    private final static String CYPHER = "RSA/ECB/OAEPWITHSHA-256ANDMGF1PADDING";

    private final static String SECURE_PASS = "";

    private final static String PASSCODE = "";

    /**
     * 代付
     *
     * @return
     */
    public static String payment(String orderSn, String merchantId, String merchantEmail,
                                 String bankName, String bankAccountNumber, String bankAccountName,
                                 String amount, String timeTranfer
    ) {
        JSONObject json = new JSONObject();
        json.put("requestId", orderSn);
        json.put("merchantId", merchantId);
        json.put("merchantEmail", merchantEmail);
        json.put("bankName", bankName);
        json.put("bankAccountNumber", bankAccountNumber);
        json.put("bankAccountName", bankAccountName);
        json.put("amount", amount);
        json.put("timeTranfer", timeTranfer);
        json.put("secureChain", encrypt(orderSn+"|" + merchantId +"|"+ amount +"|"+ bankAccountNumber +"|"+ SECURE_PASS ));
        String result = HttpUtil.htPost(SANDBOX_PAYMENT_URL, json.toString());
        return result;
    }

    public static String createVa(String merchantId,String amount,String accountVaName){
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("merchantId", merchantId);
        paramMap.put("accountVaType", "2");
        paramMap.put("accountVaName", accountVaName);
        paramMap.put("amount", amount);
        paramMap.put("condition", "10");
        String key = merchantId + "10" + PASSCODE;
        String secure_code = Md5Crypt.md5Crypt(key.getBytes(StandardCharsets.UTF_8));
        paramMap.put("secure_code", secure_code);
        String result = cn.hutool.http.HttpUtil.post(SANDBOX_CREATE_VA, paramMap);

        return result;
    }

    /**
     * 加密
     *
     * @param text
     * @return
     */
    private static String encrypt(String text) {
        String encryptedBase64 = "";
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(CRYPTO_METHOD);
            KeySpec keySpec = new X509EncodedKeySpec(Base64.decode(PRIVATE_KEY));
            Key key = keyFactory.generatePublic(keySpec);
            final Cipher cipher = Cipher.getInstance(CYPHER);
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] bytes = cipher.doFinal(text.getBytes(StandardCharsets.UTF_8));
            encryptedBase64 = new String(Base64.encode(bytes));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return encryptedBase64;
    }

    private static String decrypt(String encryptedBase64) {
        String decryptedString = "";
        try {
            KeyFactory keyFactory = KeyFactory.getInstance(CRYPTO_METHOD);
            KeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decode(PRIVATE_KEY));
            Key key = keyFactory.generatePrivate(keySpec);
            final Cipher cipher = Cipher.getInstance(CYPHER);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] decode = Base64.decode(encryptedBase64);
            byte[] bytes = cipher.doFinal(decode);
            decryptedString = new String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return decryptedString;
    }
}
